package com.example.whm;


import org.holoeverywhere.app.Activity;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.LinearLayout;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.PopupMenu;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.example.whm.lib.box_Selection;
import com.example.whm.lib.buyrecord_Selection;

public class BuyRecordActivity extends Activity implements OnClickListener,buyrecord_Selection{
  
	AQuery aq;
	private TextView one;
	private TextView four;
	private TextView three;
	private TextView two;
	private TextView five;
	
	private MyOrderFragment onef;
	private MyPreOrderFragment twof;
	private MySubScribeFragment threef;
	private FourFragment fourf;
	private FiveFragment fivef;
	private FourFragmentOrder onebox;
	private FourFragmentPreorder twobox;
	private FourFragmentSubscribe threebox;
	
	private FragmentManager fm;
	TextView title;
	View title_r;
	LinearLayout body;
	SharedPreferences sp;
	String tag;
	boolean is_detele=false;
	int box_index=0;
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setTheme(R.style.Holo_Theme_Light); 
		
		//setTheme(R.style.StyledIndicators); 
		//getSupportActionBar().hide();
		
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		// setSupportProgressBarVisibility(false);
		aq = new AQuery(this);		
		setContentView(R.layout.buy_record);
        
		fm=getSupportFragmentManager();
		
	    title = aq.id(R.id.main_top).id(R.id.buy_re_title).getTextView();
	    title_r= aq.id(R.id.main_top).id(R.id.buy_re_r).getView();
	   // title_r= aq.id(R.id.main_top).id(R.id.buy_re_r).getView();
	    aq.id(R.id.main_top).id(R.id.buy_re_r).clicked(this);
	    title.setText("my order");
		setTabSelection(0);
		
		
	/*	Bundle b=getIntent().getExtras();
		if (b!=null) {
			tag=b.getString("tag");
			if (tag.equals("box")) {
				aq.id(R.id.main_bott_four).click();
			}else if(tag.equals("profile")){
				aq.id(R.id.main_bott_five).click();
			}
		}else{
			aq.id(R.id.main_bott_one).click();
			
		}*/
		
		
		
		//  mLocations = getResources().getStringArray(R.array.locations);

	        /*Context context = getSupportActionBar().getThemedContext();
	      
	        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
	        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

	        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
	        getSupportActionBar().setListNavigationCallbacks(list, this);*/
	}

	
	public void init(){
		//aq.ajax(url, String.class, this, "parsedata");
	}
	
	
	
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {  
        case R.id.buy_re_r:
        	if (is_detele) {
        		System.out.println("box_index="+box_index);
        		switch (box_index) {  
                case 0:
                	onef.delete();
                	break;
                case 1:
                	twof.delete();
                	break;
                case 2:
                	threef.delete();
                	break;
                default:  
                    break;  }
			}else{
				PopupMenu menu = new PopupMenu(BuyRecordActivity.this,title_r);       	
	            makeMenu(menu.getMenu(), getMenuInflater());
	            menu.show();
			}
        	
        	
         break;
            
        default:  
            break;  
        }  
		
	}
	
	public void setTabSelection(int index){
        FragmentTransaction transaction = fm.beginTransaction();   
        box_index=index;
		switch (index) {  
        case 0:  
        	hideFragments(transaction); 
        	if (onef == null) {  
        		 onef = new  MyOrderFragment();  
                transaction.add(R.id.buy_re_container, onef);  
            } else {            
                transaction.show(onef);  
            } 
            
        	break;
        case 1:  
        	hideFragments(transaction); 
        	if (twof == null) {  
        		twof = new  MyPreOrderFragment();  
                transaction.add(R.id.buy_re_container, twof);  
            } else {  
                transaction.show(twof);  
            } 
        	break;
        case 2:  
        	hideFragments(transaction); 
        	if (threef == null) {  
        		threef = new  MySubScribeFragment();  
                transaction.add(R.id.buy_re_container, threef);  
            } else {  
                transaction.show(threef);  
            } 
        	System.out.println("threef==========================");
        	break;
       
        default:  
            break; 	
		}
		transaction.commit();
		
	}



	private void hideFragments(FragmentTransaction transaction) {  
		
        if (onef != null) {  
            transaction.hide(onef);  
        }  
        if (twof != null) {  
            transaction.hide(twof);  
        }  
        if (threef != null) {  
            transaction.hide(threef);  
        }  
       
       
    }
	
	 public void makeMenu(Menu menu, MenuInflater inflater) {
	      if (menu instanceof ContextMenu) {
	          ((ContextMenu) menu).setHeaderTitle("Context menu title");
	      }
	      menu.clear();
	      inflater.inflate(R.menu.menu_buy_re, menu);
	      
	      for (int i = 0; i < menu.size(); i++) {
	          menu.getItem(i).setOnMenuItemClickListener(mOnMenuItemClickListener);
	        
	      }
	     
	  }
	  
	  private final OnMenuItemClickListener mOnMenuItemClickListener = new OnMenuItemClickListener() {
	      @Override
	      public boolean onMenuItemClick(MenuItem menuItem) {
	          boolean result = false;
	          final int id = menuItem.getItemId();
	          if (menuItem.getTitle().equals("my order")) {
	        	  title.setText(menuItem.getTitle());
	        	  setTabSelection(0);
			  }
	          if (menuItem.getTitle().equals("my preorder")) {  
	        	  title.setText(menuItem.getTitle());
	        	  setTabSelection(1);
	          }
	          if (menuItem.getTitle().equals("my subscribe")) { 
	        	  title.setText(menuItem.getTitle());
	        	  setTabSelection(2);
	          }
	          //  System.out.println("onMenuItemClick id========="+id);
	          return true;
	      }
	  };





	@Override
	public void set_isdelete(boolean b) {
		// TODO Auto-generated method stub
		is_detele=b;
	}




	  
	  
	  
	  
	  
	  
}
